package com.ruoyi.equipment.controller;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.equipment.domain.TEquipment;
import com.ruoyi.equipment.domain.TEquipmentLog;
import com.ruoyi.equipment.domain.UserHint;
import com.ruoyi.equipment.mapper.TEquipmentLogMapper;
import com.ruoyi.equipment.mapper.TUserLogMapper;
import com.ruoyi.equipment.service.UserHintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/user/hint")
public class UserHintController extends BaseController {

    @Autowired
    private UserHintService userHintService;

    @Autowired
    private TEquipmentLogMapper tEquipmentLogMapper;

    @Autowired
    private TUserLogMapper tUserLogMapper;

    @GetMapping("/getUserHint")
    public AjaxResult getUserHinte() {
        Long userId = getUserId();
        UserHint userHint = userHintService.getUserHint(userId);
        //判断是否有未归还的设备
        Date newTime = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(newTime);
        now.add(Calendar.DAY_OF_MONTH, -2);
        Date oldTime = now.getTime();
        //根据日期进行个计数
        Integer count = tEquipmentLogMapper.selectTEquipmentLogByTime(userId, newTime, oldTime);
        if (count <= 0) {
            userHint.setHint(1);
        }
        return AjaxResult.success(userHint);
    }

    @GetMapping("/updateUserHint")
    public AjaxResult updateUserHint() {
        Long userId = getUserId();
        userHintService.updateUserHint(userId);
        return AjaxResult.success();
    }

    @GetMapping("/getDeptUserHint")
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    public AjaxResult getDeptUserHint() {
        Long userId = getUserId();
        UserHint userHint = userHintService.getUserHint(userId);
        Long deptId = getDeptId();
        Date newTime = new Date();
//        Calendar now = Calendar.getInstance();
//        now.setTime(newTime);
//        now.add(Calendar.DAY_OF_MONTH, -2);
//        Date oldTime = now.getTime();
        TEquipmentLog tEquipmentLog = new TEquipmentLog();
        tEquipmentLog.setDeptId(deptId.intValue());
        tEquipmentLog.setUseTime(newTime);
        int count = userHintService.selectTEquipmentLogDeptByTime(tEquipmentLog);
        if (count <= 0) {
            userHint.setHint(1);
        }
        return AjaxResult.success(userHint);
    }

}
